package com.example.androidpractice;

import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidpractice.adapter.UserListLayoutAdapter;
import com.example.androidpractice.databinding.ActivityUserListBinding;
import com.example.androidpractice.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserListActivity extends AppCompatActivity {

    List<User> users;
    UserListLayoutAdapter userListLayoutAdapter;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.rvUser)
    RecyclerView rvUser;
    @BindView(R.id.userListLayout)
    LinearLayout userListLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        users = new ArrayList<>();
        ActivityUserListBinding activityUserListBinding = DataBindingUtil.setContentView(this, R.layout.activity_user_list);
        userListLayoutAdapter = new UserListLayoutAdapter(this, users);
        activityUserListBinding.rvUser.setLayoutManager(new LinearLayoutManager(this));
        activityUserListBinding.rvUser.setAdapter(userListLayoutAdapter);
        ButterKnife.bind(this);
        //setContentView(R.layout.activity_user_list);
    }

    @OnClick(R.id.button2)
    public void onViewClicked() {
       for (int i=0;i<20;i++){

           users.add(new User(getRandomString(5),i, getRandomString(random.nextInt(20))));
       }
        userListLayoutAdapter.notifyDataSetChanged();

    }
    Random random=new Random();
    String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
     String getRandomString(int length){

        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
}
